package com.gf.service;

import com.gf.client.OrderClient;
import com.gf.client.StorageClient;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor(onConstructor_ = { @Autowired })
public class BusinessService {

	private static final Logger LOGGER = LoggerFactory.getLogger(BusinessService.class);

	private final StorageClient storageClient;

	private final OrderClient orderClient;

	/**
	 * a减库存，下订单
	 *
	 * @param userId
	 * @param commodityCode
	 * @param orderCount
	 */
	@GlobalTransactional
	@SuppressWarnings("unused")
	public void purchase(String userId, String commodityCode, int orderCount) {
		LOGGER.info("purchase begin ... xid: " + RootContext.getXID());
		storageClient.deduct(commodityCode, orderCount);
		orderClient.create(userId, commodityCode, orderCount);

		 int i=1/0;
	}
}
